home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / lex / Grammar.grm < prev    next >
Encoding:
Text File  |  1997-08-18  |  2.2 KB  |  110 lines  |  [TEXT/R*ch]

  1. /* The grammar for lexer definitions */
  2.  
  3. %{
  4. open List Syntax Gram_aux;
  5. %}
  6.  
  7. %token <string> Tident
  8. %token <char> Tchar
  9. %token <string> Tstring
  10. %token <Syntax.location> Taction
  11. %token Trule Tparse Tand Tequal Tend Tor Tunderscore Teof Tlbracket Trbracket
  12. %token Tstar Tmaybe Tplus Tlparen Trparen Tcaret Tdash
  13.  
  14. %left Tor
  15. %left CONCAT
  16. %nonassoc Tmaybe
  17. %left Tstar
  18. %left Tplus
  19.  
  20. %start lexer_definition
  21. %type <Syntax.lexer_definition> lexer_definition
  22. %type <Syntax.location> header
  23. %type <(string * (Syntax.regular_expression * Syntax.location) list) list>
  24.     other_definitions
  25. %type <string * (Syntax.regular_expression * Syntax.location) list>
  26.     definition
  27. %type <(Syntax.regular_expression * Syntax.location) list>
  28.     entry rest_of_entry
  29. %type <Syntax.regular_expression * Syntax.location> case
  30. %type <Syntax.regular_expression> regexp
  31. %type <char list> char_class char_class1
  32.  
  33. %%
  34.  
  35. lexer_definition:
  36.     header Trule definition other_definitions Tend
  37.         { Lexdef($1, $3::(rev $4)) }
  38. ;
  39. header:
  40.     Taction
  41.         { $1 }
  42.   |
  43.         { Location(0,0) }
  44. ;
  45. other_definitions:
  46.     other_definitions Tand definition
  47.         { $3::$1 }
  48.   |
  49.         { [] }
  50. ;
  51. definition:
  52.     Tident Tequal entry
  53.         { ($1,$3) }
  54. ;
  55. entry:
  56.     Tparse case rest_of_entry
  57.         { $2::rev $3 }
  58. ;
  59. rest_of_entry:
  60.     rest_of_entry Tor case
  61.         { $3::$1 }
  62.   |
  63.         { [] }
  64. ;
  65. case:
  66.     regexp Taction
  67.         { ($1,$2) }
  68. ;
  69. regexp:
  70.     Tunderscore
  71.         { Characters all_chars }
  72.   | Teof
  73.         { Characters [#"\000"] }
  74.   | Tchar
  75.         { Characters [$1] }
  76.   | Tstring
  77.         { regexp_for_string $1 }
  78.   | Tlbracket char_class Trbracket
  79.         { Characters $2 }
  80.   | regexp Tstar
  81.         { Repetition $1 }
  82.   | regexp Tmaybe
  83.         { Alternative($1, Epsilon) }
  84.   | regexp Tplus
  85.         { Sequence($1, Repetition $1) }
  86.   | regexp Tor regexp
  87.         { Alternative($1,$3) }
  88.   | regexp regexp %prec CONCAT
  89.         { Sequence($1,$2) }
  90.   | Tlparen regexp Trparen
  91.         { $2 }
  92. ;
  93. char_class:
  94.     Tcaret char_class1
  95.         { subtract all_chars $2 }
  96.   | char_class1
  97.         { $1 }
  98. ;
  99. char_class1:
  100.     Tchar Tdash Tchar char_class1
  101.         { char_class $1 $3 @ $4 }
  102.   | Tchar char_class1
  103.         { $1 :: $2 }
  104.   | /* */
  105.         { [] }
  106. ;
  107.  
  108. %%
  109.  
  110.